<?php

namespace App\Models;

use App\Exceptions\FailException;
use App\Http\Common\SignatureHelper;
use Illuminate\Database\Eloquent\Model;

/**
 * App\Models\Common
 *
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\Common newModelQuery()
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\Common newQuery()
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\Common query()
 * @mixin \Eloquent
 */
class Common extends Model
{
    public function sendSmsMsg($name,$password, $phone_number)
    {

        $accessKeyId = env('AliyunAccessKeyId');
        $accessKeySecret = env('AliyunAccessKeySecret');

        $params = [];
        $params['PhoneNumbers']= $phone_number;
        $params["SignName"] = env('AliyunSignName');
        $params["TemplateCode"] = "SMS_168820226";
        $params["TemplateParam"] =[
            'name' => $name,
            'password' => $password
        ];
        if(!empty($params["TemplateParam"]) && is_array($params["TemplateParam"])) {
            $params["TemplateParam"] = json_encode($params["TemplateParam"], JSON_UNESCAPED_UNICODE);
        }
        $helper = new SignatureHelper();

        $content = $helper->request(
            $accessKeyId,
            $accessKeySecret,
            "dysmsapi.aliyuncs.com",
            array_merge($params, array(
                "RegionId" => "cn-hangzhou",
                "Action" => "SendSms",
                "Version" => "2017-05-25",
            ))

        );
        return $content;

    }

    /**
     * 发送短信验证码
     * @param $phone_number
     * @param int $type
     * @return string
     * @throws FailException
     */
    public function sendSmsCode($phone_number, $type = 1)
    {

        $array ['$content->Code']= [
            'isp.RAM_PERMISSION_DENY' => 'sfsdfsd',

        ];
        $accessKeyId = env('AliyunAccessKeyId');
        $accessKeySecret = env('AliyunAccessKeySecret');

        $code = rand(100000,999999);
        $params = [];
        $params['PhoneNumbers']= $phone_number;
        $params["SignName"] = env('AliyunSignName');
        $params["TemplateCode"] = env('AliyunTemplateCode');
        $params["TemplateParam"] =[
            'code' => $code
        ];
        if(!empty($params["TemplateParam"]) && is_array($params["TemplateParam"])) {
            $params["TemplateParam"] = json_encode($params["TemplateParam"], JSON_UNESCAPED_UNICODE);
        }
        $helper = new SignatureHelper();

        $smsMsg = new SmsMsg();

        $smsMsg->type = $type;
        $smsMsg->dead_line = date('Y-m-d H:i:s', strtotime('+5 minute'));
        $smsMsg->mobile_phone = $phone_number;
        $smsMsg->msg_code = $code;
        $smsMsg->temp_id = $params['TemplateCode'];
        $smsMsg->data = $params['TemplateParam'];

        $content = $helper->request(
            $accessKeyId,
            $accessKeySecret,
            "dysmsapi.aliyuncs.com",
            array_merge($params, array(
                "RegionId" => "cn-hangzhou",
                "Action" => "SendSms",
                "Version" => "2017-05-25",
            ))

        );
        $smsMsg->result_info = json_encode($content, JSON_UNESCAPED_UNICODE);
        $smsMsg->save();
        if ($content->Code == 'OK'){
            $smsMsg->is_sended = 1;
            $smsMsg->save();
            return '短信验证码发送成功，请注意查收';
        }else{
            if ($content->Code == 'isv.BUSINESS_LIMIT_CONTROL'){
                throw new FailException("发送短信次数过多，请联系管理员！");
            }else{

                throw new FailException("短信验证码发送失败，请重新尝试！");
            }
        }
    }
}
